<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  
</head>
<body style="height: 3000px;">
  <div id="app">
    <button @click="change">{{isShow?'消失':'显示'}}</button>
    <keep-alive>
      <common-head v-if="isShow"></common-head>
    </keep-alive>
    
  </div>
  <script src="./vue.js"></script>
  <script>
    // 全局组件
    const CommonHead =  {
      template: `
        <div> 
          <h1>我是一个公共头部组件</h1>
          <input v-model="msg"/>
        </div>
      `,
      data(){
        return {
          msg:""
        }
      },
      activated(){
        console.log('缓存组件 被 激活时触发');
        setTimeout(()=>{
          this.msg = "请求的数据"
        },1000);
      },
      deactivated() {
        console.log('缓存组件 被 停用时触发')
      },
      beforeCreate() {
        console.log('实例初始化完成之前')
      },
      created(){
        console.log('实例初始化完成');
        setTimeout(()=>{
          this.msg = "请求的数据"
        },1000);
      },
      beforeMount(){
        console.log('实例渲染模板完成之前');
      },
      mounted() {
        console.log('模板渲染已经完成');
      },
    
      beforeDestroy() {
      
        console.log('实例销毁之前')
      },
      destroyed() {
        console.log('实例已经销毁')
      },
    }
    Vue.component('CommonHead', CommonHead)
    const vm = new Vue({
      el: '#app',
      data:{
        isShow: true
      },
      methods:{
        change(){
          this.isShow = !this.isShow;
        }
      }
      
    })
  </script>
</body>
</html>